package com.sangmo.fts.api.student;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.data.json.context.ExtViewContext;
import com.sangmo.boot.framework.data.json.meta.ExtView;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.training.interfaces.ExaminationTeamCreation;
import com.sangmo.fts.training.interfaces.TeamCreation;
import com.sangmo.fts.training.interfaces.TeamUpdate;
import com.sangmo.fts.training.model.Team;
import com.sangmo.fts.training.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@Controller
@RequestMapping("/team/v1")
@Access("team")
public class TeamApi {

    @Autowired
    private TeamService teamService;

    @Access(":create")
    @PostMapping(value = "/create")
    public @ResponseBody
    ApiResponse<Team> create(@RequestBody @Valid TeamCreation obj) {
        return ApiResponse.make(teamService.create(obj));
    }

    @Access(":read")
    @PostMapping(value = "/{id}/read")
    public @ResponseBody
    ApiResponse<Team> read(@PathVariable("id") Long id) {
        ExtViewContext.get().setMaxDepth(2);
        return ApiResponse.make(teamService.get(id));
    }


    @Access(":update")
    @PostMapping("/{id}/update")
    public @ResponseBody ApiResponse<Team> update(@PathVariable("id") Long id, @RequestBody @Valid TeamUpdate co) {
        return ApiResponse.make(teamService.update(id, co));
    }

    /**
     * 练习模式 同个班级下团队搜索
     */
    @Access(":read")
    @PostMapping("/search")
    public @ResponseBody ApiResponse<PageResult<Team>> clazzSearch(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewContext.get().setMaxDepth(2);
        return ApiResponse.make(teamService.clazzSearch(ps));
    }

    /**
     * 考核下团队创建
     */
    @Access(":create")
    @PostMapping(value = "/examination/{examinationId}/create")
    public @ResponseBody
    ApiResponse<Team> examinationCreate(@PathVariable("examinationId")Long examinationId, @RequestBody @Valid ExaminationTeamCreation obj) {
                obj.setExaminationId(examinationId);
        return ApiResponse.make(teamService.createByExaminationId(obj));
    }

    @PostMapping(value = "/examination/{examinationId}/update")
    public @ResponseBody
    ApiResponse<Team> examinationUpdate(@PathVariable("examinationId")Long examinationId, @RequestBody @Valid TeamUpdate obj) {
        return ApiResponse.make(teamService.updateByExaminationId(examinationId, obj));
    }

    /**
     * 考核下团队搜索
     */
    @Access(":read")
    @PostMapping("/examination/{examinationId}/search")
    public @ResponseBody ApiResponse<PageResult<Team>> examinationSearch(@PathVariable("examinationId")Long examinationId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
//        ps.getFilters().add(new ValueFilter("examinationId", ValueFilter.OP_EQ, examinationId));
        return ApiResponse.make(teamService.examinationSearch(examinationId, ps));
    }




}
